﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace TianYuan.Utils
{
    /// <summary>
    /// 用户中心加密解密类
    /// </summary>
    public class AES
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="content"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string encryptAES(string content, string password)
        {
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(content);
            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Convert.FromBase64String(password),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = rm.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            string encryptResultStr = byteToHexStr(resultArray);
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(encryptResultStr));
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="encryptResultStr"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string decrypt(string encryptResultStr, string password)
        {
            string decrpt = Encoding.UTF8.GetString(Convert.FromBase64String(encryptResultStr));
            byte[] decryptFrom = strToToHexByte(decrpt);
            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Convert.FromBase64String(password),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = rm.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(decryptFrom, 0, decryptFrom.Length);
            return Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
    }
}